package com.situ.springboot.controller;

import com.situ.springboot.entity.Student;
import com.situ.springboot.service.IStudentService;
import com.situ.springboot.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private IStudentService studentService;

    /*
        String pageNoStr = req.getParameter("pageNo");
        if (pageNoStr == null || pageNoStr.equals("")) {
            pageNoStr = "1";
        }
        String pageSizeStr = req.getParameter("pageSize");
        if (pageSizeStr == null || pageSizeStr.equals("")) {
            pageSizeStr = "5";
        }
        int pageNo = Integer.parseInt(pageNoStr);
        int pageSize = Integer.parseInt(pageSizeStr);*/
    @RequestMapping("/selectByPage")
    public String selectByPage(Model model, @RequestParam(defaultValue = "1") Integer pageNo, @RequestParam(defaultValue = "5")Integer pageSize) {
        System.out.println("StudentController.selectByPage");
        PageBean pageInfo = studentService.selectByPage(pageNo, pageSize);

        model.addAttribute("pageInfo", pageInfo);

        return "student_list";
    }

    @RequestMapping("/selectAll")
    @ResponseBody
    public List<Student> selectAll() {
        System.out.println("StudentController.selectAll");
        List<Student> list = new ArrayList<>();
        Student student1 = new Student(1, "zhangsan1", 23, "男");
        Student student2 = new Student(2, "zhangsan2", 23, "男");
        Student student3 = new Student(3, "zhangsan3", 23, "男");
        list.add(student1);
        list.add(student2);
        list.add(student3);

        return studentService.selectAll();
    }

    @RequestMapping("/getStudentListPage")
    public String getStudentListPage(Model model) {
        System.out.println("StudentController.getStudentListPage");
        List<Student> list = studentService.selectAll();
        model.addAttribute("list", list);
        // return "/WEB-INF/jsp/student_list.jsp";
        return "student_list";
    }
}
